﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Da.Db.POS;
using Me.Amon.OMS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdatePoint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var vip = Request["vip"];
            if (!CharUtil.IsValidateHash(vip))
            {
                return;
            }

            var dba = new RDBEngine();
            ReadVip(dba, vip);

            dba.Reset();
            ListPoints(dba, vip, 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppend_Click(object sender, EventArgs e)
        {
            var point = TbPoint.Text.Trim();
            if (!CharUtil.IsValidateLong(point))
            {
                LlErr.Text = "调整积分输入有误！";
                return;
            }
            var iPoint = int.Parse(point);
            var remark = TbRemark.Text;

            var dba = new RDBEngine();
            dba.AddTable(DCustomerPoint.TABLE);
            dba.AddParam(DCustomerPoint.ID, HashUtil.UtcTimeInHex());
            dba.AddParam(DCustomerPoint.CHANNEL, CPos.CHANNEL_NAME);
            dba.AddParam(DCustomerPoint.CUSTOMER_ID, HfId.Value);
            dba.AddParam(DCustomerPoint.CREATE_TIME, DCustomerPoint.NOW, false);
            dba.AddParam(DCustomerPoint.POINT, point, false);
            dba.AddParam(DCustomerPoint.SOURCE, CCustomerPoint.SOURCE_ADJUST);
            dba.AddParam(DCustomerPoint.REMARK, remark);
            dba.AddInsertBatch();

            dba.Reset();
            dba.AddTable(DVip.TABLE);
            dba.AddParam(DVip.POINT, DVip.POINT + (iPoint > 0 ? "+" : "") + iPoint, false);
            dba.AddWhere(DVip.ID, HfId.Value);
            dba.AddUpdateBatch();

            dba.ExecuteBatch();

            dba.Reset();
            ListPoints(dba, HfId.Value, 0);
            LlPoint.Text = (int.Parse(LlPoint.Text) + iPoint).ToString();
            TbPoint.Text = "0";
            TbRemark.Text = "";
        }

        protected void BnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Vips.aspx?lev=" + HfLevel.Value);
        }
        #endregion

        private void ReadVip(RDBEngine dba, string vip)
        {
            dba.AddTable(DVip.TABLE);
            dba.AddWhere(DVip.ID, vip);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HfId.Value = vip;
            LlCode.Text = "" + row[DVip.CODE];
            LlName.Text = "" + row[DVip.DISPLAY_NAME];
            LlBirthday.Text = "" + row[DVip.BIRTHDAY];
            LlCellphone.Text = "" + row[DVip.CELLPHONE];
            LlTelephone.Text = "" + row[DVip.TELEPHONE];
            HlLevel.Text = "" + row[DVip.LEVEL_NAME];
            HlLevel.NavigateUrl = "Vips.aspx?lev=" + row[DVip.LEVEL_ID];
            HfLevel.Value = "" + row[DVip.LEVEL_ID];
            LlPoint.Text = "" + row[DVip.POINT];
        }

        private void ListPoints(RDBEngine dba, string vip, int page)
        {
            dba.AddTable(DCustomerPoint.TABLE);
            dba.AddColumn(DCustomerPoint.ID);
            dba.AddColumn(DCustomerPoint.CREATE_TIME);
            dba.AddColumn(DCustomerPoint.POINT);
            dba.AddColumn(DCustomerPoint.SOURCE);
            dba.AddColumn(DCustomerPoint.SO_ID);
            dba.AddColumn(DCustomerPoint.SO_CODE);
            dba.AddColumn(DCustomerPoint.REMARK);
            dba.AddWhere(DCustomerPoint.CHANNEL, CPos.CHANNEL_NAME);
            dba.AddWhere(DCustomerPoint.CUSTOMER_ID, vip);
            dba.AddSort(DCustomerPoint.CREATE_TIME, false);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;
            //if (cnt < 1)
            //{
            //    RpVips.DataSource = table;
            //    RpVips.DataBind();
            //    return;
            //}

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpPoints.DataSource = pds;
            RpPoints.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            var lev = HfLevel.Value;
            ListPoints(dba, lev, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            var dba = new RDBEngine();
            var lev = HfLevel.Value;
            ListPoints(dba, lev, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            var dba = new RDBEngine();
            var lev = HfLevel.Value;
            ListPoints(dba, lev, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            var dba = new RDBEngine();
            var lev = HfLevel.Value;
            ListPoints(dba, lev, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            var dba = new RDBEngine();
            var lev = HfLevel.Value;
            ListPoints(dba, lev, max);
        }
        #endregion
    }
}